package SM.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import SM.BusinessLogic.DBAccess;
import SM.BusinessLogic.DBService;
import SM.BusinessLogic.SessionVariables;


/**
 * Servlet implementation class ReportIssue
 */

public class BindUI extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public BindUI() {
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		String id = request.getParameter("id");
		
		if(id == null)
		{
			return;
		}
		
		if(id.equals("soc_name"))
			this.BindSocietyName(request,out);
		
		if(id.equals("user"))
			this.BindUser(request,response);
		
		if(id.equals("role"))
			this.BindRole(request, response);
		
		if(id.equals("dept"))
			this.BindDept(request,response);
	}
	
	private void BindDept(HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		PrintWriter out = response.getWriter();
		
		String str = "";
		
		if((str = SessionVariables.GetDept(request)) != null)
		{
			out.println(str);
			return;
		}
		
		String user = SessionVariables.GetUser(request);
		
		if(user == null || user == "")
			this.BindUser(request, response);
		
		String selectQuery = "select resp_name "+
				"from CommitteeInfo C inner join Responsibilities R on C.resp_id = R.resp_id " +
				"where C.email = ?";
		
		synchronized(this) { 
			
			Connection connection = null;
			PreparedStatement statement = null;
		/*Prolog*/
			try{
				
				connection = DBAccess.MakeConnection();
				statement = connection.prepareStatement(selectQuery);
				statement.setString(1, user);
				
				ResultSet resultSet = DBService.ExecuteStatement(connection, statement);
		
		/*Actual table data*/
				
				if(resultSet.next())
				{
					String dept = resultSet.getString(1);
					out.println(dept);
					SessionVariables.SetDept(dept, request);
				}
				
			}catch(SQLException SQLEx)
			{
				System.out.println("SQLException in BindUI\nMessage : " + SQLEx.getMessage());
			}
			finally{
		/*Epilogue*/
				DBAccess.CloseConnection(connection);
				DBAccess.CloseStatement(statement);
			}
		}
		
	}

	private void BindRole(HttpServletRequest request, HttpServletResponse response) throws IOException{
		
		PrintWriter out = response.getWriter();
		boolean isHomePage = false;
		
		String home = request.getParameter("home");
		
		if(home == null)
			isHomePage = false;
		else if(home.equals("1"))
			isHomePage = true;
		else
		{
			return;
		}
		
		String user = SessionVariables.GetUser(request);
		
		if(user == null || user == "")
			this.BindUser(request, response);
		
		String selectQuery = "select * from CommitteeInfo where email = ?";
		
		synchronized(this) { 
			
				String strRole = "";
				
				if(DBService.IsRecordPresent(selectQuery, user))
				{
					strRole = "<a href=\"../Issues/issues-1.html\">Pending Issues</a>";
				}
				
				if(isHomePage && !strRole.equals(""))
					strRole = "<a href=\"Issues/issues-1.html\">Pending Issues</a>"; //for home page this url is used
				
				out.println(strRole);
		}
	}

	private void BindUser(HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		String user = SessionVariables.GetUser(request);
		
		if(user == null || user == "")
			response.sendRedirect("index.html");
		
		PrintWriter out = response.getWriter();
		
		out.println(user);
	}

	private void BindSocietyName(HttpServletRequest request,PrintWriter out) {
		String soc = new String();
		
		if((soc = SessionVariables.GetSocName(request)) != null)
		{
			out.println(soc);
			return;
		}
			
		synchronized(this) { 
			
				Connection connection = null;
				CallableStatement statement = null;
			/*Prolog*/
				try{
					connection = DBAccess.MakeConnection();
					statement = DBAccess.GetStatementForProcedureCall(connection, "SocietyInfoSelect()");
			
					ResultSet resultSet = DBService.ExecuteProcedure(connection, statement);
			
			/*Actual table data*/
					
					if(resultSet.next())
					{
						String socName = resultSet.getString(1);
							
						out.println(socName);
					
						SessionVariables.SetSocName(socName, request);
					}
					
				}catch(SQLException SQLEx)
				{
					System.out.println("SQLException in BindUI\nMessage : " + SQLEx.getMessage());
				}
				finally{
			/*Epilogue*/
					DBAccess.CloseConnection(connection);
					DBAccess.CloseStatement(statement);
				}
			}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// doPost is used to Accept request from page and Make Changes
		
	}
}
